package com.zoe.onelink.upms.common.entity;

import com.zoe.onelink.upms.common.constants.SystemConstants;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>标题: OneLink统一认证用户，实现UserDetails接口，适配0Auth2协议</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件</p>
 *
 * @author LinHongKai
 * @date 2022-02-23 16:26
 */
@Data
public class UserDetailsInfo implements UserDetails {

    private static final long serialVersionUID = 8605205124139090310L;

    public UserDetailsInfo(){}

    public UserDetailsInfo(String username, String password, String validFlag){
        this.username = username;
        this.password = password;
        this.authorities = Collections.unmodifiableSet(new HashSet<>());
        this.enabled = SystemConstants.VALID_FLAG_ENABLE.equals(validFlag);
        this.accountNonExpired = true;
        this.credentialsNonExpired = true;
        this.accountNonLocked = true;
    }

    // UserDetails字段
    private String username;
    private String password;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;

    // 是否为超级管理员
    private Boolean isAdministrator;

    // 系统用户字段
    // 用户ID
    private String userNo;

    // 用户名
    private String userUsername;

    // 工牌号
    private String workerNo;

    // 用户姓名
    private String name;

    // 性别字典的键值
    private String sexCode;

    // 性别字典的键值对应的标签
    private String sexLabel;

    // 手机号
    private String mobile;

    // 邮箱
    private String email;

    // 状态，0：启用；1：禁用
    private String validFlag;

    // 登录页选择的院区编码
    private String districtCode;

    // 登录页选择的院区名称
    private String districtName;

    // 登录页选择的机构编码
    private String orgCode;

    // 登录页选择的机构名称
    private String orgName;

    /**
     * 原始UserDetails接口的权限字段，此字段在RedisTokenStore序列化时，会使得序列化后的字符串文本太大，已弃用<br/>
     * 现改为，单独一份存储在redis中，当接口需要鉴权时才从redis中读取进行校验
     */
    @Deprecated
    private Set<GrantedAuthority> authorities;

    /**
     * 原始UserDetails接口的权限字段，此字段在RedisTokenStore序列化时，会使得序列化后的字符串文本太大，已弃用<br/>
     * 现改为，单独一份存储在redis中，当接口需要鉴权时才从redis中读取进行校验
     * @return
     */
    @Override
    @Deprecated
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return new HashSet<>();
    }
}
